namespace CIS.Customer
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class CustomerInfoPreferencePage : Page
    {
        protected Button AddBtn;
        protected Image iBtnCustomer;
        protected ImageButton iBtnGray2;
        protected ImageButton iBtnOrange0;
        protected ImageButton iBtnOrange1;
        protected ImageButton iBtnOrange3;
        protected ImageButton iBtnOrange4;
        protected ImageButton iBtnOrange5;
        protected ImageButton iBtnOrange6;
        protected ImageButton iBtnOrange7;
        protected ImageButton iBtnOrange8;
        protected CheckBox IsOtherChk;
        protected Label lbOther;
        protected Label lbPreLevel;
        protected Label lbPreType;
        protected Label lbPreValue;
        protected TextBox OtherTxb;
        protected DropDownList preLevelDdl;
        protected DropDownList preTypeDdl;
        protected DropDownList preValueDdl;
        protected UserPrincipal Principal = null;
        protected Repeater rptPre;

        private void AddBtn_Click(object sender, EventArgs e)
        {
            Guid preTypeId;
            Guid preValueId;
            Guid preLevelId;
            string strOtherPre = this.OtherTxb.Text.Trim();
            if (!this.IsOtherChk.Checked)
            {
                preTypeId = new Guid(this.preTypeDdl.SelectedItem.Value);
                preValueId = new Guid(this.preValueDdl.SelectedItem.Value);
                preLevelId = new Guid(this.preLevelDdl.SelectedItem.Value);
            }
            else
            {
                preTypeId = new Guid(this.preTypeDdl.SelectedItem.Value);
                preValueId = new Guid(this.preValueDdl.SelectedItem.Value);
                preLevelId = new Guid(this.preLevelDdl.SelectedItem.Value);
            }
            ManagePreference.InsertNewCustomerPreferenceInfo(new Guid(base.Request.QueryString["CustomerId"]), preTypeId, preValueId, preLevelId, strOtherPre);
            this.FillrptPre();
            ManageLog.InsertLog(this.Principal.SysUser.UserId, this.Principal.SysUser.LoginName, DateTime.Now, "InsertPre", "IP:" + base.Request.UserHostAddress + " DNS:" + base.Request.UserHostName, ManageCustomer.GetCustomerBasicInfoViaId(new Guid(base.Request.QueryString["CustomerId"])).Name);
        }

        private void FillPreferenceLevel(Guid preferenceTypeId)
        {
            string language = this.Session["language"].ToString();
            this.preLevelDdl.DataSource = ManagePreference.GetPreferenceLevel(preferenceTypeId);
            switch (language)
            {
                case "cn":
                    this.preLevelDdl.DataTextField = "PreferenceLevelNameCn";
                    break;

                case "en":
                    this.preLevelDdl.DataTextField = "PreferenceLevelNameEn";
                    break;
            }
            this.preLevelDdl.DataValueField = "PreferenceLevelId";
            this.preLevelDdl.DataBind();
        }

        private void FillPreferenceValue(Guid preferenceTypeId)
        {
            string language = this.Session["language"].ToString();
            this.preValueDdl.DataSource = ManagePreference.GetPreferenceValue(preferenceTypeId);
            switch (language)
            {
                case "cn":
                    this.preValueDdl.DataTextField = "PreferenceValueNameCn";
                    break;

                case "en":
                    this.preValueDdl.DataTextField = "PreferenceValueNameEn";
                    break;
            }
            this.preValueDdl.DataValueField = "PreferenceValueId";
            this.preValueDdl.DataBind();
        }

        private void FillrptPre()
        {
            Guid customerId = new Guid(base.Request.QueryString["CustomerId"]);
            if ((this.Principal.SysUser.IsAdmin <= 0) && !ManageSystemConfig.IsThisCustomerMaster(this.Principal.SysUser.UserId, customerId))
            {
                this.AddBtn.Visible = false;
                this.IsOtherChk.Visible = false;
                this.OtherTxb.Visible = false;
            }
            this.rptPre.DataSource = ManagePreference.GetCustomerPreferenceInfo(new Guid(base.Request.QueryString["CustomerId"]));
            this.rptPre.DataBind();
        }

        private void iBtnOrange0_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/CIS/Customer/CustomerBasicInfoPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange1_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/CIS/Customer/CustomerInfoPersonalPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange3_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/CIS/Customer/CustomerInfoHealthCostumePage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange4_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/CIS/Customer/CustomerInfoTravelPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange5_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/CIS/Customer/CustomerInfoStudyPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange6_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/CIS/Customer/CustomerInfoWorkPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange7_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/CIS/Customer/CustomerInfoFamilyPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange8_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/CIS/Customer/CustomerInfoOtherPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void InitializeComponent()
        {
            this.iBtnOrange0.Click += new ImageClickEventHandler(this.iBtnOrange0_Click);
            this.iBtnOrange1.Click += new ImageClickEventHandler(this.iBtnOrange1_Click);
            this.iBtnOrange3.Click += new ImageClickEventHandler(this.iBtnOrange3_Click);
            this.iBtnOrange4.Click += new ImageClickEventHandler(this.iBtnOrange4_Click);
            this.iBtnOrange5.Click += new ImageClickEventHandler(this.iBtnOrange5_Click);
            this.iBtnOrange6.Click += new ImageClickEventHandler(this.iBtnOrange6_Click);
            this.iBtnOrange7.Click += new ImageClickEventHandler(this.iBtnOrange7_Click);
            this.iBtnOrange8.Click += new ImageClickEventHandler(this.iBtnOrange8_Click);
            this.preTypeDdl.SelectedIndexChanged += new EventHandler(this.preTypeDdl_SelectedIndexChanged);
            this.IsOtherChk.CheckedChanged += new EventHandler(this.IsOtherChk_CheckedChanged);
            this.AddBtn.Click += new EventHandler(this.AddBtn_Click);
            this.rptPre.ItemCommand += new RepeaterCommandEventHandler(this.rptPre_ItemCommand);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            try
            {
                string language = this.Session["language"].ToString();
                this.OtherTxb.Visible = false;
                Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
                this.preTypeDdl.DataSource = ManagePreference.GetPreferenceType(area[0].AreaId);
                switch (language)
                {
                    case "cn":
                        this.preTypeDdl.DataTextField = "PreferenceNameCn";
                        break;

                    case "en":
                        this.preTypeDdl.DataTextField = "PreferenceNameEn";
                        break;
                }
                this.preTypeDdl.DataValueField = "PreferenceId";
                this.preTypeDdl.DataBind();
                this.FillPreferenceValue(new Guid(this.preTypeDdl.SelectedItem.Value));
                this.FillPreferenceLevel(new Guid(this.preTypeDdl.SelectedItem.Value));
                this.FillrptPre();
            }
            catch (Exception)
            {
            }
        }

        private void IsOtherChk_CheckedChanged(object sender, EventArgs e)
        {
            if (this.IsOtherChk.Checked)
            {
                this.preValueDdl.Enabled = false;
                this.preLevelDdl.Enabled = false;
                this.OtherTxb.Text = string.Empty;
                this.OtherTxb.Visible = true;
                this.preLevelDdl.SelectedIndex = 0;
                this.preValueDdl.SelectedIndex = 0;
            }
            else
            {
                this.preValueDdl.Enabled = true;
                this.preLevelDdl.Enabled = true;
                this.OtherTxb.Text = string.Empty;
                this.OtherTxb.Visible = false;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.InitPage();
            }
        }

        private void preTypeDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.FillPreferenceValue(new Guid(this.preTypeDdl.SelectedItem.Value));
            this.FillPreferenceLevel(new Guid(this.preTypeDdl.SelectedItem.Value));
        }

        private void rptPre_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (this.Principal.OwnRole.TypeB == 0)
            {
                base.Response.Write("<script> alert('没有权限操作！') </script>");
            }
            else
            {
                Guid customerId = new Guid(base.Request.QueryString["CustomerId"]);
                if ((this.Principal.SysUser.IsAdmin <= 0) && !ManageSystemConfig.IsThisCustomerMaster(this.Principal.SysUser.UserId, customerId))
                {
                    base.Response.Write("<script> alert('没有权限操作！') </script>");
                }
                else
                {
                    string str;
                    string cmd = e.CommandName;
                    string strTmp = (string) e.CommandArgument;
                    Guid customerPreferenceId = new Guid(strTmp);
                    if (((str = cmd) != null) && (string.IsInterned(str) == "delete"))
                    {
                        ManagePreference.RemoveCustomerPreferenceInfoViaId(customerPreferenceId);
                        this.InitPage();
                        ManageLog.InsertLog(this.Principal.SysUser.UserId, this.Principal.SysUser.LoginName, DateTime.Now, "DeletePre", "IP:" + base.Request.UserHostAddress + " DNS:" + base.Request.UserHostName, ManageCustomer.GetCustomerBasicInfoViaId(new Guid(base.Request.QueryString["CustomerId"])).Name);
                    }
                }
            }
        }

        protected string SelectLanguage(string preType)
        {
            string language = this.Session["language"].ToString();
            if (preType == "PreferenceNameCn")
            {
                switch (language)
                {
                    case "cn":
                        return "PreferenceNameCn";

                    case "en":
                        return "PreferenceNameEn";
                }
            }
            else if (preType == "PreferenceValueNameCn")
            {
                switch (language)
                {
                    case "cn":
                        return "PreferenceValueNameCn";

                    case "en":
                        return "PreferenceValueNameEn";
                }
            }
            else if (preType == "PreferenceLevelNameCn")
            {
                switch (language)
                {
                    case "cn":
                        return "PreferenceLevelNameCn";

                    case "en":
                        return "PreferenceLevelNameEn";
                }
            }
            return "";
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbPreType.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoPreferencePage", "lbPreType", language, path);
            this.lbPreValue.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoPreferencePage", "lbPreValue", language, path);
            this.lbPreLevel.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoPreferencePage", "lbPreLevel", language, path);
            this.lbOther.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoPreferencePage", "lbOther", language, path);
            this.IsOtherChk.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoPreferencePage", "IsOtherChk", language, path);
            this.AddBtn.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoPreferencePage", "AddBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iBtnCustomer.ImageUrl = "../images/title_customerinfo.jpg";
                    this.iBtnOrange0.ImageUrl = "../images/title_CustomerInfo_lable_1_orange.jpg";
                    this.iBtnOrange1.ImageUrl = "../images/title_CustomerInfo_lable_2_orange.jpg";
                    this.iBtnGray2.ImageUrl = "../images/title_CustomerInfo_lable_3_Gray.jpg";
                    this.iBtnOrange3.ImageUrl = "../images/title_CustomerInfo_lable_4_orange.jpg";
                    this.iBtnOrange4.ImageUrl = "../images/title_CustomerInfo_lable_5_orange.jpg";
                    this.iBtnOrange5.ImageUrl = "../images/title_CustomerInfo_lable_6_orange.jpg";
                    this.iBtnOrange6.ImageUrl = "../images/title_CustomerInfo_lable_7_orange.jpg";
                    this.iBtnOrange7.ImageUrl = "../images/title_CustomerInfo_lable_8_orange.jpg";
                    this.iBtnOrange8.ImageUrl = "../images/title_CustomerInfo_lable_9_orange.gif";
                    break;

                case "en":
                    this.iBtnCustomer.ImageUrl = "../images/title_customerinfoEn.jpg";
                    this.iBtnOrange0.ImageUrl = "../images/title_CustomerInfoEn_lable_1_orange.jpg";
                    this.iBtnOrange1.ImageUrl = "../images/title_CustomerInfoEn_lable_2_orange.jpg";
                    this.iBtnGray2.ImageUrl = "../images/title_CustomerInfoEn_lable_3_Gray.jpg";
                    this.iBtnOrange3.ImageUrl = "../images/title_CustomerInfoEn_lable_4_orange.jpg";
                    this.iBtnOrange4.ImageUrl = "../images/title_CustomerInfoEn_lable_5_orange.jpg";
                    this.iBtnOrange5.ImageUrl = "../images/title_CustomerInfoEn_lable_6_orange.jpg";
                    this.iBtnOrange6.ImageUrl = "../images/title_CustomerInfoEn_lable_7_orange.jpg";
                    this.iBtnOrange7.ImageUrl = "../images/title_CustomerInfoEn_lable_8_orange.jpg";
                    this.iBtnOrange8.ImageUrl = "../images/title_CustomerInfoEn_lable_9_orange.gif";
                    break;
            }
        }
    }
}

